# -*- coding: utf-8 -*-
# @Time    : 2021/11/13 19:49
# @Author  : travel2040
# @Email   : travel2040@163.com
# @File    : opencv-播放视频.py
# 需要安装的库
# pip install opencv_python

import cv2

def play(url):
    cap = cv2.VideoCapture(url)
    fps = cap.get(cv2.CAP_PROP_FPS) # 视频FPS
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    w_resize = 800
    h_resize = int(height / width * w_resize)

    print("视频尺寸 宽:%d,高:%d"%(width,height))
    print("修改后视频尺寸 宽:%d,高:%d"%(w_resize,h_resize))

    while True:
        ret, frame = cap.read()

        frame = cv2.resize(frame, (w_resize,h_resize), interpolation=cv2.INTER_NEAREST)
        # 调整窗口大小
        # cv2.namedWindow("frame", 0)  # 0可调大小，注意：窗口名必须imshow里面的一窗口名一直
        # cv2.resizeWindow("frame", width,height)  # 设置长和宽
        cv2.imshow("frame", frame)
        delay = int(1000 / int(fps))

        cv2.waitKey(delay)  # 1s内每帧延迟时间（毫秒）

        if cv2.waitKey(1) & 0xFF == ord('q'):
            cap.release()
            cv2.destroyAllWindows()
            break

if __name__ == '__main__':
    # url = "data/test.mp4"
    url = "D:\\file\\data\\hb.mp4"
    # url = 'rtsp://admin:a12345678@127.0.0.1'
    # url = 'rtmp://58.200.131.2:1935/livetv/hunantv'  # 湖南卫视

    play(url)





